﻿@using IEMSOFT.EasyHotel.Admin.Models;
@using IEMSOFT.EasyHotel.Common;
@using IEMSOFT.Foundation;
@{
    ViewBag.Title = "房间管理";
}
@section header
{
}
@{
    var currentUser = ViewBag.CurrentUser as UserModel;
}
<table id="dg" title="房间管理" class="easyui-datagrid"
    url="/room/get"
    toolbar="#toolbar" pagination="false"
    rownumbers="true" fitcolumns="true" singleselect="false" ctrlselect="true" striped="true">
    <thead>
        <tr>
            <th field="roomNo">房间号</th>
            <th field="roomTypeName">房型</th>
        </tr>
    </thead>
</table>

<div id="toolbar">
    <a href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-add" plain="true" onclick="newAdd()">批量生成</a>
    <a href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-edit" plain="true" onclick="edit()">编辑</a>
    <a href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-remove" plain="true" onclick="destory()">删除</a>
</div>

<div id="dlgAdd" class="easyui-dialog" style="width: 400px; height: 200px; padding: 10px 20px"
    closed="true" buttons="#dlg-buttons">
    <form id="fmAdd" method="post" data-options="novalidate:true">
        <div class="fitem">
            <label>起始编号</label>
            <input name="fromRoomNo" class="easyui-numberbox" required="true" />
        </div>
        <div class="fitem">
            <label>房间数:</label>
            <input name="roomCount" class="easyui-numberbox" required="true" />
        </div>
        <div class="fitem">
            <label>房型:</label>
            <select id="cbRoomTypeAdd" class="easyui-combobox" name="roomTypeId"
                data-options="url:'/roomtype/option',method:'get',valueField:'value',textField:'text'">
            </select>
        </div>
    </form>
</div>

<div id="dlgUpdate" class="easyui-dialog" style="width: 400px; height: 200px; padding: 10px 20px"
    closed="true" buttons="#dlg-buttonsUpdate">
    <form id="fmUpdate" method="post" data-options="novalidate:true">
        <input name="roomId" type="hidden" />
        <div class="fitem">
            <label>房间号:</label>
            <input name="roomNo" class="easyui-numberbox" required="true" id="txtRoomNo"  />
        </div>
        <div class="fitem">
            <label>房型:</label>
            <select id="cbRoomTypeUpdate" class="easyui-combobox" name="roomTypeId"
                data-options="url:'/roomtype/option',method:'get',valueField:'value',textField:'text'">
            </select>
        </div>
    </form>
</div>
<div id="dlg-buttons">
    <a href="javascript:void(0)" class="easyui-linkbutton c6" iconcls="icon-ok" onclick="save()" style="width: 90px">保存</a>
    <a href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-cancel" onclick="javascript:$('#dlgAdd').dialog('close')" style="width: 90px">取消</a>
</div>
<div id="dlg-buttonsUpdate">
    <a href="javascript:void(0)" class="easyui-linkbutton c6" iconcls="icon-ok" onclick="save()" style="width: 90px">保存</a>
    <a href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-cancel" onclick="javascript:$('#dlgUpdate').dialog('close')" style="width: 90px">取消</a>
</div>
@section Scripts
{
<script type="text/javascript">
    var msgTitle = '房间管理';
    var actionUrl = '';
    var action = "";
    function newAdd() {
        action = "add";
        $('#dlgAdd').dialog('open').dialog('setTitle', '新增房间');
        $('#fmAdd').form('clear');
        actionUrl = "/room/add";
    }
    function edit() {
        action = "update";
        var row = $('#dg').datagrid('getSelected');
        if (row) {
            $('#dlgUpdate').dialog('open').dialog('setTitle', '编辑房间');
            $('#txtRoomNo').textbox('readonly', true);
            $('#fmUpdate').form('load', row);
            actionUrl = "/room/update";
        }
    }
    var data = "";
    function save() {
        if (action == "add") {
            if (!$('#fmAdd').form('enableValidation').form('validate')) {
                return;
            }
            if ($('#cbRoomTypeAdd').combobox('getValue') == '') {
                $.messager.alert(msgTitle, '请选择房型！', 'error');
                return;
            }
            data= $('#fmAdd').serialize();
        }
        else if (action == "update") {
            if (!$('#fmUpdate').form('enableValidation').form('validate')) {
                return;
            }
            if ($('#cbRoomTypeUpdate').combobox('getValue') == '') {
                $.messager.alert(msgTitle, '请选择房型！', 'error');
                return;
            }
            data= $('#fmUpdate').serialize();
        }
        $.post(actionUrl,
                  data,
                  function (data) {
                      var msg = '';
                      if (data.msg.length != 0) {
                          $.each(data.msg, function () {
                              msg = data.msg + msg + '<br/>';
                          });
                          $.messager.alert(msgTitle, msg, 'error');
                      }
                      else {
                          $.messager.alert(msgTitle, '提交成功！', 'info');
                          if (action == 'add') {
                              $('#dlgAdd').dialog('close'); // close the dialog
                          }
                          else if (action == 'update') {
                              $('#dlgUpdate').dialog('close'); // close the dialog
                          }
                          $('#dg').datagrid('reload'); // reload data
                      }
                  });
    }
    function destory() {
        actionUrl = '/room/remove';
        var rows = $('#dg').datagrid('getSelections');
        if (rows.length > 0) {
            $.messager.confirm('Confirm', '确实要删除吗?', function (r) {
                if (r) {
                    $.post(actionUrl,
                          JSON.stringify(rows),
                          function (data) {
                              var msg = '';
                              if (data.msg.length != 0) {
                                  $.each(data.msg, function (index, item) {
                                      msg = msg + item + '<br/>';
                                  });
                                  $.messager.alert(msgTitle, msg, 'error');
                              }
                              else {
                                  $.messager.alert(msgTitle, '提交成功！', 'info');
                                  if (action == 'add')
                                  {
                                      $('#dlgAdd').dialog('close'); // close the dialog
                                  }
                                  else if (action == 'update')
                                  {
                                      $('#dlgUpdate').dialog('close'); // close the dialog
                                  }                            
                                  $('#dg').datagrid('reload'); // reload data
                              }
                          });
                }
            });
        }
    }
</script>
}
